home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0122_Fields in BASM.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  2KB  |  73 lines

  1. ==============================================================
  2.                   PASCAL ARTICLE FOR PAPER
  3.                      by Charlie Calvert
  4. ==============================================================
  5.  
  6.         When Pascal programmers are using BASM, they are often
  7. baffled when it comes time to address a field of an object. The
  8. issue is that the field is usually located at an offset from the
  9. segment of the pointer to that object, not from the data segment of
  10. the program. As a result, a viable way to address a field of the
  11. object is to first load its object's segment and offset into ES:DI.
  12. You can use the following line of code to do this:
  13.     [--]
  14.     les di, Self
  15.     [--]
  16.    Once you are properly addressing the object itself, then all you
  17. need do is calculate the offset of the field from the beginning of
  18. the object. This can be done by adding the proper number of bytes
  19. to ES:DI. For instance, if an object has three fields, all two bytes in
  20. size, then the offset of the third field would be four:
  21.     [--]
  22.     mov ax, word [es:di + 4]
  23.     [--]
  24.     If these kinds of calculations bother you, you can avoid them by
  25. loading the segment and offset of the object directly into the data
  26. segement, but you should be sure to save its original value on the
  27. stack, with a push and a pop.
  28.     A simple illustration of the above principlese would be an object
  29. with 3 fields, the last being an integer called M. This example
  30. simply moves the value of that field into the AX register:
  31.  
  32. ==============================================================
  33.                         PASCAL EXAMPLE
  34. ==============================================================
  35. program AsmObj;
  36.  
  37. type
  38.   PMyObject = ^TMyObject;
  39.   TMyObject = Object
  40.     i,j: Word;
  41.     M: Integer;
  42.     procedure Foo;
  43.     procedure Foo1;
  44.   end;
  45.  
  46. procedure TMyObject.Foo;
  47. begin
  48.   asm
  49.     les di, Self
  50.     mov ax, word [es:di + 4]
  51.   end;
  52. end;
  53.  
  54. procedure TMyObject.Foo1;
  55. begin
  56.   asm
  57.     push ds
  58.     lds di, Self
  59.     mov ax, word ptr [di + M]
  60.     pop ds
  61.   end;
  62. end;
  63.  
  64. var
  65.   O: PMyObject;
  66.  
  67. begin
  68.   New(O);
  69.   O^.M := 10;
  70.   O^.Foo;
  71.   O^.Foo1;
  72.   Dispose(O);
  73. end.